package cn.stylefeng.guns.modular.rest;


import cn.stylefeng.guns.comm.util.DateUtils;
import cn.stylefeng.guns.modular.wordstudy.entity.*;
import cn.stylefeng.guns.modular.wordstudy.model.params.PlanParam;
import cn.stylefeng.guns.modular.wordstudy.model.params.StudyHistoryParam;
import cn.stylefeng.guns.modular.wordstudy.model.result.StudyHistoryResult;
import cn.stylefeng.guns.modular.wordstudy.service.*;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * 绑定控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/word")
public class ApiNeedTokenWordController extends BaseController {

    @Autowired
    private WordService wordService;
    @Autowired
    private StudyHistoryService studyHistoryService;
    @Autowired
    private FavoriteService favoriteService;
    @Autowired
    private SentenceService sentenceService;
    @Autowired
    private TransService transService;

    @PostMapping(value = "update")
    public Object update(Word word) {
        Word word1 = wordService.getOne(new QueryWrapper<Word>().eq("name", word.getName()));
        word1.setNameCn(word.getNameCn());
        wordService.updateById(word1);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "detail")
    public Object detail(String word, String openId) {
        Map map = new HashMap();
        Word wordObj = wordService.getOne(new QueryWrapper<Word>().eq("name", word));

        // 是否收藏
        Favorite favorite = favoriteService.getOne(new QueryWrapper<Favorite>().eq("open_id", openId).eq("word_id", wordObj.getId()));
        if (favorite != null) {
            map.put("favoriteBtnDisplay", false);
        }

        // 例句
        List<Sentence> sentenceList = sentenceService.list(new QueryWrapper<Sentence>().eq("word_id", wordObj.getId()));

        // 翻译
        List<Trans> transList = transService.list(new QueryWrapper<Trans>().eq("word_id", wordObj.getId()));

        map.put("sentenceList", sentenceList);
        map.put("transList", transList);
        map.put("word", wordObj);
        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "random")
    public Object random(String type, String openId) {
        Map map = new HashMap();

        // 随机得到一个新单词
        List<Word> words = wordService.list(new QueryWrapper<Word>().eq("type", type));
        Random random = new Random();
        int n = random.nextInt(words.size());
        Word word = words.get(n);

        // 学习数据
        StudyHistoryParam param = new StudyHistoryParam();
        param.setOpenId(openId);
        StudyHistoryResult studyHistoryResult = studyHistoryService.studyData(param);


        map.put("word", word);
        map.put("total", studyHistoryResult.getTotal());
        map.put("newWord", studyHistoryResult.getNewWord());
        map.put("oldWord", studyHistoryResult.getOldWord());

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

}


